{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# change to project root (using docker)\n",
    "import os\n",
    "os.chdir(\"..\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# imports\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from bert_keras import BertEncoder, InputExample"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def initialize_vars(sess):\n",
    "    sess.run(tf.local_variables_initializer())\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    sess.run(tf.tables_initializer())\n",
    "    tf.keras.backend.set_session(sess)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize session\n",
    "config = tf.ConfigProto()\n",
    "config.gpu_options.per_process_gpu_memory_fraction = 0.5  # change this to fit your needs\n",
    "sess = tf.Session(config=config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# load encoder with basic parameters (BERT uncased 1024)\n",
    "enc = BertEncoder()\n",
    "# initialize tf variables in session (must be after prev. line)\n",
    "initialize_vars(sess=sess)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "examples = np.empty([2], dtype=InputExample)\n",
    "\n",
    "# long sequence\n",
    "examples[0] = InputExample(\n",
    "    text_a=\"Hurricane Patricia was the most intense tropical cyclone on record worldwide in terms of wind speed and the second-most intense on record worldwide in terms of pressure, behind Typhoon Tip in 1979, with a minimum atmospheric pressure of 872 mbar. Originating from a sprawling disturbance near the Gulf of Tehuantepec, south of Mexico, in mid-October 2015, Patricia was first classified a tropical depression on October 20. \",\n",
    "    text_b=\"Initial development was slow, with only modest strengthening within the first day of its classification. The system later became a tropical storm and was named Patricia, the twenty-fourth named storm of the annual hurricane season. Exceptionally favorable environmental conditions fueled explosive intensification on October 22.\"\n",
    ")\n",
    "\n",
    "# short sequence\n",
    "examples[1] = InputExample(\n",
    "    text_a=\"Hurricane Patricia was the most intense tropical cyclone on record worldwide in terms of wind speed and the second-most intense on record worldwide in terms of pressure, behind Typhoon Tip in 1979, with a minimum atmospheric pressure of 872 mbar.\",\n",
    "    text_b=\"Initial development was slow, with only modest strengthening within the first day of its classification. \"\n",
    ")\n",
    "\n",
    "embeddings = enc.calculate_embeddings(examples)\n",
    "\n",
    "print(embeddings.shape)\n",
    "\n",
    "print(embeddings[0])\n",
    "print(embeddings[1])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
